<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/11/7
 * Time: 15:09
 */
namespace app\index\controller;

use app\common\model\OpinionModel;
use app\common\model\ProjectModel;
use app\common\model\ReplyModel;
use app\common\model\Slide;
use mg_api\Mg_api;

class Project extends Common{
    //列表
    public function index(){
        //banner
        $banner = Slide::all();

        //获取积分已发放项目
        if(empty($_SESSION['openid'])){
            $this->error('没有获取到openid');
        }
     /*   $mg_api = new Mg_api();
        $data = [
          'key'=>'Get_Project_List',
            'wx_openid'=>$_SESSION['openid'],
            'types'=>'全部'
        ];
        //获取全部项目（本地）


        $projects = $mg_api->api($data);
        $projects = json_decode($projects,true);*/
        $projects = ProjectModel::where('user_id',$_SESSION['user_id'])->select();
        $this->assign('projects',$projects);
        $this->assign('banner',$banner);

        return view('index');
    }
   // 详情
    public function details($opinion_id,$project_name){
        $project_name = mb_convert_encoding($project_name, "utf-8", "gbk");
        $mg_api = new Mg_api();
        $data=[
            'key'=>'Get_Pic',
           'address'=>$project_name
        ];
        $detail = $mg_api->api($data);
        $detail = json_decode($detail,true);
        //只需要9张图
        $imgs = [];
        if($detail['JData']){
            $i=0;
            foreach($detail['JData'] as $v){
                if($i<9){
                    $imgs[] = $v['f_storagepath'];
                    $i++;
                }
            }
        }
        $this->assign('detail',$detail);
        $this->assign('imgs',$imgs);
        $this->assign('opinion_id',$opinion_id);
        $this->assign('project_name',$project_name);
        return view('details');
    }
    //项目回复
    public function reply_project($opinion_id){
		$project = '';
		$reply = '';
        if($opinion_id){
            //获取项目信息
            $project =  ProjectModel::get($opinion_id);
				if($project){
					//获取项目回复
				$opinion = OpinionModel::where('project_id',$project->address)->find();
					if($opinion){
						 //获取反馈的title
					$title_name = feedback_type_1();
					$reply = ReplyModel::where('opinion_id',$opinion->id)->find();
			
						//如果此详细是未查看的就改变状态
					if($reply and $reply->is_look == '0'){
						$up_reply = ReplyModel::get($reply->id);
						$up_reply->is_look = '1';
						$up_reply->save();
						
					}
					$reply->title = $title_name[$opinion->title];
				}
           
			}
		
            $this->assign('project',$project);
            $this->assign('reply',$reply);
            return view('reply_project');
        }else{
            $this->error('没有获取到项目id');
        }
    }

}